package mds.chenji.against.command;

import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import mds.chenji.against.gameroom.GameRoomManager;
import mds.chenji.against.gameroom.GlobalRoomGUI;

public class CommandAgainst implements CommandExecutor {

	@Override
	public boolean onCommand(CommandSender arg0, Command arg1, String arg2, String[] arg3) {
		if (!(arg0 instanceof Player)) {
			arg0.sendMessage("错误只有玩家才能执行这个命令");
			return true;
		}
		Player p = (Player) arg0;
		if (arg3.length == 1) {
			if (arg3[0].equalsIgnoreCase("Help")) {
				p.sendMessage("§c-------------------§e红蓝对抗 帮助§c-------------------");
				p.sendMessage("§c制作：ChenJi");
				p.sendMessage("§c/against join [RoomID] §a-加入某个房间。");
				p.sendMessage("§c/against Hall §a-打开大厅菜单。");
				p.sendMessage("§c/against Exit §a-离开加入的游戏。");
				p.sendMessage("§c/against invent [PlayerName] §a-向一个玩家发送邀请。");
				p.sendMessage("§c/against accept §a-接受邀请");
				p.sendMessage("§c----------------------------------------------------");
				return true;
			}
			if (arg3[0].equalsIgnoreCase("Hall")) {
				p.openInventory(GlobalRoomGUI.getinv());
				return true;
			}
			if (arg3[0].equalsIgnoreCase("Exit")) {
				if (GameRoomManager.PlayerList.contains(p)) {
					GameRoomManager.QuitPlayer(p);
					p.sendMessage("§a[§c红§b蓝§a对抗] §c操作成功");
					return true;
				} else {
					p.sendMessage("§a[§c红§b蓝§a对抗] §c你没有加入任何房间");
					return true;
				}
			}

			if (arg3[0].equalsIgnoreCase("accept")) {
				if (GameRoomManager.invention.containsKey(p)) {
					if (GameRoomManager.PlayerList.contains(p)) {
						p.sendMessage("§a[§c红§b蓝§a对抗] §c你已经参与到了游戏中啊。");
						return true;
					}
					GameRoomManager.tryJoin(p, GameRoomManager.invention.get(p));
					GameRoomManager.invention.remove(p);
					return true;
				} else {
					p.sendMessage("§a[§c红§b蓝§a对抗] §c你没有收到邀请。");
					return true;
				}
			}

		}
		if (arg3.length == 2) {
			if (arg3[0].equalsIgnoreCase("join")) {
				try {
					short s = Short.valueOf(arg3[1]);
					if (GameRoomManager.RoomMap.containsKey(s) == false) {
						p.sendMessage("§a[§c红§b蓝§a对抗] §c没有找到那个房间");
						return true;
					}
					if (GameRoomManager.PlayerList.contains(p)) {
						p.sendMessage("§a[§c红§b蓝§a对抗] §c你已经参与到了游戏中啊。");
						return true;
					}
					GameRoomManager.tryJoin(p, s);
					return true;
				} catch (Exception e) {
					p.sendMessage("§a[§c红§b蓝§a对抗] §c没有找到那个房间");
					return true;
				}

			}
			if (arg3[0].equalsIgnoreCase("invent")) {
				try {
					if (Bukkit.getPlayer(arg3[1]) == null) {
						p.sendMessage("§a[§c红§b蓝§a对抗] §c玩家未在线。");
						return true;
					} else {
						if (GameRoomManager.PlayerList.contains(p)) {
							short s = GameRoomManager.PlayerMap.get(p);
							Player pa = Bukkit.getPlayer(arg3[1]);
							pa.sendMessage("§a[§c红§b蓝§a对抗] §c玩家 §a" + p.getName() + " §c邀请了你去红蓝对抗房间 §a" + s
									+ " §c，输入/against accept接受邀请。");
							p.sendMessage("§a[§c红§b蓝§a对抗] §c邀请成功");
							GameRoomManager.invention.put(pa, s);
							return true;
						} else {
							p.sendMessage("§a[§c红§b蓝§a对抗] §c你不在游戏中");
							return true;
						}
					}
				} catch (Exception e) {
					p.sendMessage("§a[§c红§b蓝§a对抗] §c没有找到那个房间");
					return true;
				}
			}
		}

		p.sendMessage("§a[§c红§b蓝§a对抗] §c你输入的指令有误，请输入/against help获取 ");
		return true;

	}

}
